﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sage.DownloadLite.Utils
{
    /// <summary>
    /// 字节大小格式化工具
    /// </summary>
    internal static class BytesFormatter
    {
        /// <summary>
        /// 格式化字节大小为可读字符串
        /// </summary>
        /// <param name="bytes">字节数</param>
        /// <returns>格式化后的字符串</returns>
        public static string Format(long bytes)
        {
            if (bytes >= 1073741824) // 1GB = 1024^3
                return $"{bytes / 1073741824.0:F2} GB";
            if (bytes >= 1048576) // 1MB = 1024^2  
                return $"{bytes / 1048576.0:F2} MB";
            if (bytes >= 1024) // 1KB = 1024
                return $"{bytes / 1024.0:F2} KB";

            return $"{bytes} Bytes";
        }
        /// <summary>
        /// 格式化速度（字节/秒）
        /// </summary>
        /// <param name="bytesPerSecond">每秒字节数</param>
        /// <returns>格式化后的速度字符串</returns>
        public static string FormatSpeed(double bytesPerSecond)
        {
            if (bytesPerSecond >= 1073741824)
                return $"{bytesPerSecond / 1073741824.0:F2} GB/s";
            if (bytesPerSecond >= 1048576)
                return $"{bytesPerSecond / 1048576.0:F2} MB/s";
            if (bytesPerSecond >= 1024)
                return $"{bytesPerSecond / 1024.0:F2} KB/s";

            return $"{bytesPerSecond:F0} B/s";
        }
    }
}
